//
//  MainViewController.m
//  localServerDemo
//
//  Created by 夏利兵 on 2020/9/24.
//  Copyright © 2020 夏利兵. All rights reserved.
//

#import "MainViewController.h"
#import "LBXFileDownServer.h"
#import "LBXFileUpServer.h"
#import "LBXDAVServer.h"
#import "LBXLoggerRealServer.h"

@interface MainViewController ()
@property (weak, nonatomic) IBOutlet UILabel *urlLabel;
@property (weak, nonatomic) IBOutlet UITextField *portTextField;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self copyResource];
}

- (IBAction)startFileDownServer:(id)sender
{
    NSString* tmpDir =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    [LBXFileDownServer sharedManager].fileDir = tmpDir;
    
    if( [[LBXFileDownServer sharedManager]start])
    {
        self.urlLabel.text = [[LBXFileDownServer sharedManager]visitUrl];
    }
    else
    {
        self.urlLabel.text = [LBXFileDownServer sharedManager].errMsg;
    }
    
}

- (IBAction)startFileUpServer:(id)sender {
    
    NSString* tmpDir =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    [LBXFileUpServer sharedManager].fileDir = tmpDir;
    
    if( [[LBXFileUpServer sharedManager]start])
    {
        self.urlLabel.text = [[LBXFileUpServer sharedManager]visitUrl];
        
    }
    else
    {
        self.urlLabel.text = [LBXFileUpServer sharedManager].errMsg;
    }
}

- (IBAction)startDAV:(id)sender {
    
    NSString* tmpDir =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    [LBXDAVServer sharedManager].fileDir = tmpDir;
    
    if( [[LBXDAVServer sharedManager]start])
    {
        self.urlLabel.text = [[LBXDAVServer sharedManager]visitUrl];
    }
    else
    {
        self.urlLabel.text = [LBXDAVServer sharedManager].errMsg;
    }
}

- (IBAction)startloggerServer:(id)sender {

    if([[LBXLoggerRealServer sharedManager]start])
    {
        self.urlLabel.text = [[LBXLoggerRealServer sharedManager]visitUrl];
    }
    else
    {
        self.urlLabel.text = [LBXLoggerRealServer sharedManager].errMsg;
    }
}

- (IBAction)testAddLog:(id)sender {
    
    static NSInteger num = 10000;
    
    NSString *msg = [NSString stringWithFormat:@"iOSLog:%ld",(long)num++];
    
    //prefix已经替换NSLog
    NSLog(@"%@",msg);

}

- (IBAction)stopAll:(id)sender
{
    [[LBXFileDownServer sharedManager]stop];
    
    [[LBXFileUpServer sharedManager]stop];
    
    [[LBXDAVServer sharedManager]stop];
    
    [[LBXLoggerRealServer sharedManager]stop];
}

#pragma mark- copy资源到 document
- (void)copyResource
{
    //主bundle，也就是可执行的工程的bundle
    NSBundle *mainBundle = [NSBundle mainBundle];
    //放在主工程中的自定义bundle
    NSString *myBundlePath = [mainBundle pathForResource:@"H5Resource" ofType:@"bundle"];
    //    NSBundle *myBundle = [NSBundle bundleWithPath:myBundlePath];
    //放在自定义bundle中的图片
    //    NSString *imagePath = [myBundle pathForResource:@"123" ofType:@"png"];
        
    //html中加载的资源(如图片等)，只能临时目录
  
    NSString* tmpDir =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];


    NSString *dstPath = [tmpDir stringByAppendingPathComponent:@"h5Resource"];
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:dstPath error:nil];
    
    
    NSString *cacheImgsPath = [dstPath stringByAppendingPathComponent:@"html"];
    if(![[NSFileManager defaultManager] fileExistsAtPath:cacheImgsPath])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:cacheImgsPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    [self copyFileFromPath:myBundlePath toPath:dstPath];
    
}

- (BOOL)deleteAllFileAtPath:(NSString *)aPath
{
    BOOL result = NO;
    NSArray *fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:aPath
    error:nil];
    
    NSString *filePath = nil;
    
    for (int i = 0; i<[fileArray count]; i++)
    {
        filePath = [aPath stringByAppendingPathComponent:[fileArray objectAtIndex:i]];
        result = [[NSFileManager defaultManager] removeItemAtPath:filePath
                                                     error:nil];
        if (!result)
        {
            break;
        }
        filePath = nil;
    }
    return result;
}

+ (NSArray *)getContentsOfDirectoryAtPath:(NSString *)aDirString
{
    return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:aDirString
                                                        error:nil];
}

- (void)copyFileFromPath:(NSString *)sourcePath toPath:(NSString *)toPath
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    
    NSArray* array = [fileManager contentsOfDirectoryAtPath:sourcePath error:nil];
    
    for(int i = 0; i<[array count]; i++)
    {
        
        NSString *fullPath = [sourcePath stringByAppendingPathComponent:[array objectAtIndex:i]];
        
        NSString *fullToPath = [toPath stringByAppendingPathComponent:[array objectAtIndex:i]];
        
//        NSLog(@"%@",fullPath);
        
//        NSLog(@"%@",fullToPath);
        
        //判断是不是文件夹
        BOOL isFolder = NO;
        
        //判断是不是存在路径 并且是不是文件夹
        BOOL isExist = [fileManager fileExistsAtPath:fullPath isDirectory:&isFolder];
        
        if (isExist)
        {
            NSError *err = nil;
            
            [[NSFileManager defaultManager] copyItemAtPath:fullPath toPath:fullToPath error:&err];
            
            if (err) {
//                NSLog(@"%@",err);
            }
            
            if (isFolder)
            {
                [self copyFileFromPath:fullPath toPath:fullToPath];
            }
        }
    }
}

@end
